<?php
/**
 * 《产品客户》实现类
 *
 * @author 孙晓晔
 * @version $Id: Customer.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/ContactUtil.class.php');

class Customer extends ContactUtil {
	public $tbl = Constant::tbl_customer;

	public $info = '1';
	
	public $status = array(
		'-' => '请选择',
		'1' => '活跃RFQ',
		'2' => '活跃PO',
		'3' => '不活跃RFQ',
		'4' => '不活跃PO',
			
		// '9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'name' => '名称',
		'country' => '国家',
		'star' => '等级',
	
		'emp' => '业务员',
		'oper' => '录入员',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'star' => '等级',
		'emp' => '业务员',
		'country' => '国家',
		'status' => '状态',
			
		'2' => '日期',
		'3' => '月份',
		'4' => '年份',
	);

	public $continent = array(
		'-' => '请选择',
		'1' => '亚洲',
		'2' => '欧洲',
		'3' => '美洲',	
		'4' => '非洲',
		'5' => '大洋洲',	
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('continent_option', $this->continent);
		
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['客户管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {
		$data = array(
			'客户信息' => array('cc'=>'contact.php?do=Customer.view_code', 'js'=>'1'),
			'客户管理' => array('cc'=>'contact.php?do=Customer.find', 'js'=>'1'),
			'编号信息' => array('cc'=>'customer.php?do=Code.find', 'js'=>''),
				
			'报价信息' => array('cc'=>'customer.php?do=Quote.find', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=Mold.find', 'js'=>'1'),
			'统计分析' => array('cc'=>'contact.php?do=Customer.tj_tab', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'客户信息' => array('cc'=>'contact.php?do=Customer.group', 'js'=>''),
			'邮件地址' => array('cc'=>'customer.php?do=Email.group', 'js'=>''),
			'报价项目' => array('cc'=>'customer.php?do=Quote.group', 'js'=>''),			
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}
	
	function view_code($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'contact/customer/code');
		return true;
	}
	
	function view_codePost($controller, $request) {
	
		$code_s = $request['code_s'];
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code_s);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
				
			if (!$this->_view_code_my($row)) {
				$controller->assign('msg', "对不起，操作受限，请联系管理员。");
					
				$controller->display($request, 'info');
				return true;
			}
				
			$row = $this->_view($row['id']);
				
			$controller->assign_by_ref('result', $row);
				
			$ret = $this->_view_tab();
				
			$controller->assign_by_ref('tabs', $ret);
				
			$controller->display($request, 'contact/customer/view');
		}
	
		return true;
	}
	
	function _view_code_my($data) {
	
		if ($this->_is_admin()) {
			return true;
		}
	
		if ($this->_audit('25') || $this->_audit('76')) {
			return true;
		}
	
		$emp = $_SESSION['id'];
	
		if ($data['emp'] == $emp || $data['engineer'] == $emp || $data['oper'] == $emp) {
			return true;
		}
	
		return false;
	
	}
	
	function _view_tab() {
		$data = array(
			'联系人' => array('cc'=>'info.php?do=Contact.view_customer&id=', 'js'=>'1', 'v'=>'1'),

			'报价信息' => array('cc'=>'customer.php?do=QuoteMy.view_customer&id=', 'js'=>'1'),
			'产品信息' => array('cc'=>'mold.php?do=MoldMy.view_customer&id=', 'js'=>'1'),

			'银行到帐' => array('cc'=>'finance.php?do=TransactionMy.view_customer&id=', 'js'=>'1'),
			'收入信息' => array('cc'=>'finance.php?do=InMy.view_customer&id=', 'js'=>'1'),
				
			'附件信息' => array('cc'=>'contact.php?do=Customer.view_file&id=', 'js'=>'1'),
		);
	
		return $this->_tab2($data);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		
		$star_op = array(
			'<' => '小于',
			'<=' => '小于或等于',
			'=' => '等于',
			'>' => '大于',
			'>=' => '大于或等于',
		);
		$controller->assign('star_op', $star_op);
		
		$emp = array(
			// '-' => '请选择',
			
			'emp' => '业务员',
			'oper' => '录入员',
			'bj' => '报价员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'contact/customer/find');
		return true;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		$emp = $_SESSION['id'];
		
		$p = array('1'=>'161', '2'=>'162', '3'=>'173');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
				
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return sprintf("visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function _where($request) {
		$continent_s = $request['continent_s'];
		$country_s = $request['country_s'];

		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$star_s = $request['star_s'];
		$star_op_s = $request['star_op_s'];
		if (empty($star_op_s)) {
			$star_op_s = "=";
		}
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'bj') {
			$request['bj_s'] = $request['emp_a_s'];
		}
		
		$bj_s = $request['bj_s'];
				
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$s = "%" . $name_s . "%";
			$where .= sprintf(" AND (name LIKE '%s' OR fullname LIKE '%s' OR url LIKE '%s' OR jj LIKE '%s' OR contact LIKE '%s')", $s, $s, $s, $s, $s);
		}

		if (!empty($star_s) && $star_s <> '-') {
			$where .= sprintf(" AND star %s '%s'", $star_op_s, $star_s);
		}

		if (!empty($continent_s) && $continent_s <> '-') {
			$where .= " AND continent = '" . $continent_s . "'";
		}

		if (!empty($country_s) && $country_s <> '-') {
			$where .= " AND country = '" . $country_s . "'";
		}
		
		if (!empty($bj_s) && $bj_s <> '-') {
			$where .= " AND bj = '" . $bj_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['contact_'] = $this->_num_contact($row['id']);
				$row['bank_'] = $this->_num_bank($row['id']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'contact/customer/list');
		return true;
	}
	
	function _num_contact($id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
	
		return $aa->num('1', $id);
	}
	
	function _num_bank($id) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$aa = new Bank();
	
		return $aa->num('3', $id);
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		if (!empty($id)) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id(Constant::tbl_email, $id);

			$controller->assign_by_ref('email', $row);
		}

		$controller->assign_by_ref('country_option', $this->code_option(Constant::tbl_country, '1'));

		$controller->display($request, 'contact/customer/add');
		return true;
	}

	function email($controller, $request) {

		$id = $request['id'];
		clean(Constant::tbl_email . "_id_" . $id);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_email, $id);
		debug($sql);
		$db->query($sql);

		return $this->addPost($controller, $request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$code = $request['code'];
		$code_t = $request['code_t'];
		if ($code_t == '1') {
			require_once(dirname(__FILE__) . '/../customer/Code.class.php');
			$aa = new Code();
			$code = $aa->get();
		}
		
		$name = addslashes($request['name']);
		$fullname = addslashes($request['fullname']);
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);
		
		$continent = $request['continent'];
		$country = $request['country'];
		
		$address = addslashes($request['address']);
		$url = strtolower($request['url']);
		
		$jj = addslashes($request['jj']);
		
		$emp = $_SESSION['id'];
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, cert_type, cert_no, name, fullname, continent, country, address, url, jj, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $cert_type, $cert_no, $name, $fullname, $continent, $country, $address, $url, $jj, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		
		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);
		
		$request['id_s'] = $id;

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert($controller, $request) {
		$code = $request['code'];
		$code_t = $request['code_t'];
		if ($code_t == '1') {
			require_once(dirname(__FILE__) . '/../customer/Code.class.php');
			$aa = new Code();
			$code = $aa->get();
		}
		
		$name = addslashes($request['name']);

		$fullname = addslashes($request['fullname']);
		$continent = $request['continent'];
		$country = $request['country'];

		$address = addslashes($request['address']);
		$url = strtolower($request['url']);

		$jj = addslashes($request['jj']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, fullname, continent, country, address, url, jj, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $fullname, $continent, $country, $address, $url, $jj, $_SESSION['id'], $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		if (empty($code)) {
			$code = $id;
		}
		
		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);

		$contact = stripslashes($request['contact']);
		$this->_contact($id, $contact);
		
		return $id;
	}
	
	function _contact1($info, $info_id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_linkman_primary($info, $info_id);
	}
	
	
	function _contact($id, $contact) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_insert($this->info, $id, $contact);
	}
	
	function _contact_x($id, $contact) {
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET contact = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $contact, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _contact_id($id, $contact) {
		$this->_contact_x($id, $contact);
		
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_update2($this->info, $id, $contact);
	}

	function _is_insert($v) {
		if (empty($v->linkman)) {
			return false;
		}

		if (!empty($v->tel)) {
			return true;
		}

		if (!empty($v->mobile)) {
			return true;
		}

		if (!empty($v->email)) {
			return true;
		}

		return false;
	}
	
	function _delete($z) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_delete($z);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('country_option', $this->code_option(Constant::tbl_country));

		$controller->assign_by_ref('contact', $this->_linkman($id));

		$controller->display($request, 'contact/customer/edit');
		return true;
	}

	function _linkman($id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		$ret = $aa->_ajax_linkman('1', $id);
		
		return $this->_shift($ret);
	}
	
	function _quote($id) {
		require_once(dirname(__FILE__) . '/../customer/Quote.class.php');
		$aa = new Quote();
		
		return $aa->_list_3($id);
	}
	
	function _mold($id) {
		require_once(dirname(__FILE__) . '/../mold/Mold.class.php');
		$aa = new Mold();
		
		return $aa->_list_3($id);
	}
	
	function _transaction($id) {
		require_once(dirname(__FILE__) . '/../finance/Transaction.class.php');
		$aa = new Transaction();
		
		return $aa->_list_3($id);
	}
	
	function _initem($id) {
		require_once(dirname(__FILE__) . '/../finance/InItem.class.php');
		$aa = new InItem();
		
		return $aa->_list_3($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		
		if (empty($code)) {
			require_once(dirname(__FILE__) . '/../customer/Code.class.php');
			$aa = new Code();
			$code = $aa->get();
		}
		
		$name = addslashes($request['name']);
		$fullname = addslashes($request['fullname']);
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);

		$continent = $request['continent'];
		$country = $request['country'];

		$address = addslashes($request['address']);
		$geo = strtolower($request['geo']);
		$url = strtolower($request['url']);

		$jj = addslashes($request['jj']);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET cert_type = '%s', cert_no = '%s', name = '%s', fullname = '%s', continent = '%s', country = '%s', address = '%s'";
		$fmt .= ", geo = '%s', url = '%s', jj = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $cert_type, $cert_no, $name, $fullname, $continent, $country, $address, $geo, $url, $jj, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		// debug($sql);
		$db->query($sql);

		$contact = stripslashes($request['contact']);
		$this->_contact_id($id, $contact);

		$z = stripslashes($request['z']);
		$this->_delete($z);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'contact/customer/upload');
		return true;
	}
	
	function bj($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$result = $this->_view($id);
	
		$controller->assign_by_ref('result', $result);
		$controller->assign_by_ref('customer', $result);
	
		$controller->assign_by_ref('emp_option', $this->emp_dept('scbj'));
	
		$controller->display($request, 'contact/customer/bj');
		return true;
	}
	
	function bjPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$bj = $request['bj'];
		if (empty($bj)) {
			$bj = $_SESSION['id'];
		}
	
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET bj = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $bj, $id);
		// debug($sql);
		$db->query($sql);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);
		
		$ret = $this->_view_tab();
		
		$controller->assign_by_ref('tabs', $ret);
		
		if (!empty($request['small'])) {
			$controller->display($request, 'contact/customer/view_small');
			return true;
		}

		$controller->display($request, 'contact/customer/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['cert_type_t'] = $this->cert_type[$row['cert_type']];

		$row['c'] = $this->_contact1($this->info, $row['id']);
		
		$country = $this->code(Constant::tbl_country, $row['country']);
		if ($country) {
			$row['country_t'] = $country['name'] . '[' . $country['zone'] . ']';
		}
		
		$row['bj_t'] = Name::emp($row['bj']);
		
		// $row['files'] = $this->_files($row['id']);

		$row = $this->_view_t($row);

		return $row;
	}
	
	function _view_code($s) {
		if (empty($s)) {
			return false;
		}
		
		$a = preg_split("/[\.]+/", $s);
		$code = $a[0];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			return $row;
		}
		
		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'contact/customer/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		$sql = sprintf($fmt, $this->s, $this->tbl);
		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'country') {
			$row['a'] = $this->name_code(Constant::tbl_country, $row['a']);
		} else {
			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}
	
	function ajax_project($controller, $request) {
		$project = $request['project'];
		
		$pos = strpos($project, ".");
		if ($pos === false) {
			$code = $project;
		} else {
			$code = substr($project, 0, $pos);
		}

		$s = sprintf("project = %s, code = %s, pos = %s\n", $project, $code, $pos);
		debug($s);
		
		$row = $this->code($this->tbl, $code);
		
		debug(print_r($row, true));
		
		echo json_encode($row);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_my($controller, $request) {
		$data = $this->_ajax_code($request, '2');
		echo json_encode($data);
	}
	
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request, $my = '1') {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		if ($my == '2') {
			$key = sprintf("%s_%s_%s_%s", $this->tbl, "code_my", $_SESSION['id'], $q);
		} else {
			$key = sprintf("%s_%s_%s", $this->tbl, "code_all", $q);
		}
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		if ($my == '2') {
			$fmt = "SELECT * FROM %s WHERE visible = 1 AND code LIKE '%s' AND emp = '%s' ORDER BY id DESC LIMIT %s";
			$sql = sprintf($fmt, $this->tbl, "%" . $q . "%", $_SESSION['id'], $n);
		} else {
			$fmt = "SELECT * FROM %s WHERE visible = 1 AND code LIKE '%s' ORDER BY id DESC LIMIT %s";
			$sql = sprintf($fmt, $this->tbl, "%" . $q . "%", $n);
		}
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_continent($controller, $request) {
		$this->pretreat($controller);

		$continent = $request['continent'];

		$key = "customer.continent_" . $continent;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				$controller->assign('result', json_encode($ret));

				$request['tpl'] = 'json';
				$controller->display($request, 'json');
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE visible = 1 AND continent = '%s'";
		$sql = sprintf($fmt, $this->tbl, $continent);
		// debug($sql);
		$result = $db->query($sql);
		$ret = $db->fetch_all($result);
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		$controller->assign('result', json_encode($ret));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_exist($controller, $request) {
		$code = $request['code'];
		if (empty($code)) {
			echo "true";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		//debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}
	
	function _exist($code) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}
		
		return false;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}

	function _delete2($id) {
		$db = newdb('emp');

		$fmt = "DELETE FROM %s WHERE info = '%s' AND info_id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $this->info, $id);
		// debug($sql);
		$db->query($sql);
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('301');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '客户情况';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'contact/customer/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('301');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '客户情况（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);
		
		$body = $this->_tj_html($controller, $request, 'contact/customer/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}

	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function ajax_id($controller, $request) {
		$id = $this->_input($request['id']);
		
		$row = $this->id($this->tbl, $id);
		
		echo json_encode($row);
	}
	
	function ajax_linkman($controller, $request) {
		$q = $this->_input($request['q']);
		
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		$data = $aa->_ajax_linkman(1, $q);
		echo json_encode($data);
	}
	
	function connect($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('customer', $row);
		
		$controller->display($request, 'contact/customer/connect');
		return true;
	}
	
	function connectPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];	// 客户	
		$to = $this->_input($request['to_emp']);
		
		require_once(dirname(__FILE__) . '/../customer/Quote.class.php');
		$quote = new Quote();
		$quote->move_id_emp($id, $to);
				
		require_once(dirname(__FILE__) . '/../mold/Mold.class.php');
		$mold = new Mold();
		$mold->move_id($id, $to);
		
		require_once(dirname(__FILE__) . '/../finance/In.class.php');
		$in = new In();
		$in->move_id($id, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Transaction.class.php');
		$tr = new Transaction();
		$tr->move_id($id, $to);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $id);
		debug($sql);
		$db->query($sql);
				
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;		
	}
	
	function _customer_id($code) {
		$row = Common::code($this->tbl, $code);
		
		return $row['id'];
	}
	
	function _x_rmb($id, $rmb) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET x_rmb = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $rmb, $id);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function _star($id, $star) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET star = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $star, $id);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function bj222($controller, $request) {
		$this->pretreat($controller);
		
		require_once(dirname(__FILE__) . '/../customer/Quote.class.php');
		$quote = new Quote();
		
		require_once(dirname(__FILE__) . '/../mold/Mold.class.php');
		$mold = new Mold();
		
		$db = newdb($this->dsn_w);
		// $fmt = "SELECT * FROM %s WHERE status in ('1', '2')";
		$fmt = "SELECT * FROM %s WHERE status in ('1', '2') AND createtime > ADDDATE(now(), INTERVAL -1 MONTH) AND createtime < ADDDATE(now(), INTERVAL -1 WEEK)";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {			
			if ($quote->_exist($row['id'])) {
				continue;
			}
			
			if ($mold->_exist($row['id'])) {
				continue;
			}
			
			echo "U";
			$this->_update_status($row['id'], '3');
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		// $fmt = "SELECT * FROM %s LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $s);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			
			$a = $this->_linkman($row['id']);
			
			// print_r($a);
			
			$b = $this->_abc($a);
			
			// print_r($b);
			
			// $c = json_encode($b);
			
			arrayRecursive($b, 'urlencode', true);
			$json = json_encode($b);
			$c = urldecode($json);
			
			$fmt = "UPDATE %s SET contact = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $c, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
			
			echo ".";
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function _abc($data) {
		
		$ret = array();
		$a = array();
		while (list($k, $v) = each($data)) {
			
			$a['name'] = $v['name'];
			$a['job'] = $v['job'];
			$a['mobile'] = $v['mobile'];
			$a['dh'] = $v['dh'];
			$a['tel'] = $v['tel'];
			$a['fax'] = $v['fax'];
			$a['email'] = $v['email'];
			
			$a['address'] = $v['address'];
			$a['qq'] = $v['qq'];
			$a['skype'] = $v['skype'];
			$a['content'] = $v['content'];
			
			$ret[] = $a;
		}
		
		return $ret;
	}
	
	function c($controller, $request) {
		$s = "createtime > DATE_ADD(now(), INTERVAL -6 MONTH)";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE visible = '1' AND %s";
		$sql = sprintf($fmt, $this->tbl, $s);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($this->_imperfect($this->info, $row['id'])) {
				$this->_c_notice($controller, $request, $row['id']);
			}
		}

		logdate($controller, $request);
		return true;
	}
	
	function _c_notice($controller, $request, $id) {		
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);		
		$controller->assign_by_ref('contact', $this->_linkman($id));
		
		$from = '101';
		$to = $row['emp'];
		// $to = '101';
		$subject = sprintf('客户 %s 信息不完善', $row['code']);
		$body = $this->_tj_html($controller, $request, 'contact/customer/body_x');
		// echo $body;
					
		$this->_notice($from, $to, $subject, $body, 1);
		
		if ($row['emp'] <> $row['oper']) {
			$this->_notice($from, $row['oper'], $subject, $body, 1);
		}
	}
	
	function _imperfect($info, $info_id) {
		require_once(dirname(__FILE__) . '/../info/Contact.class.php');
		$aa = new Contact();
		
		return $aa->_imperfect($info, $info_id);
	}
	
	function tecmold() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code LIKE 'TEC__'";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['id']] = $row['name'];
		}
		
		return $ret;
	}
	
	function bank($controller, $request) {
	
		$id = $request['id'];
		
		$controller->assign_by_ref('customer', $this->_view($id));
	
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
	
		$bank->customer($controller, $request);
	}
	
	function bankPost($controller, $request) {
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
	
		$bank->customerPost($controller, $request);
	
		$this->bank($controller, $request);
	}
	
	function ajax_bank($controller, $request) {
		$name = $request['name'];
		$code = $request['code'];
	
		require_once(dirname(__FILE__) . '/../finance/Bank.class.php');
		$bank = new Bank();
	
		$ret = array('status'=>'1');
		if ($bank->_name_code($name, $code)) {
			$ret['status'] = '2';
		}
	
		echo json_encode($ret);
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'contact/customer/_list');
		return true;
	}
	
	function smtp_t($controller, $request) {
		$this->pretreat($controller);
	
		$data = $this->smtp_d();
	
		print_r($data);
		
		logdate($controller, $request);
		return true;
	}
	
	function smtp($controller, $request) {
		$this->pretreat($controller);
		
		$request['tpl'] = "html4";
		
		$data = $this->smtp_d();
		
		$controller->assign_by_ref('result', $data);
	
		$body = $controller->sunxy($request, 'contact/customer/smtproute');
		
		echo $body;
	}
	
	function smtp_d() {
	
		$ret = array();
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE star > 1";
		// $fmt = "SELECT * FROM %s LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $s);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
	
			// print_r($row);
				
			$a = json_decode($row['contact'], true);
			// print_r($a);
				
			$b = $this->smtp_a($a);
			if (count($b) > 0) {
				$ret = array_merge($ret, $b);
			}
	
			// echo ".";
		}
		
		$ret = array_unique($ret);
		
		return $ret;
	}
	
	function smtp_a($a) {
		
		$ret = array();
		while (list($k, $v) = each($a)) {
			$email = $v['email'];
			if (empty($email)) {
				continue;
			}
			
			$domain = $this->smtp_b($email);
			if (empty($domain)) {
				continue;
			}
			
			$ret[] = $domain;
		}
		
		$ret = array_unique($ret);
		
		// print_r($ret);
		
		return $ret;
	}
	
	function smtp_b($s) {
		
		$a = preg_split("/@/", $s);
		
		if (count($a) > 1) {
			return $a[1];
		}
		
		return '';
	}
}
?>
